package src.java.easy;

import java.util.HashSet;

/**
 * @author 袁敏
 * @className Solution160
 * @date 2024/7/28
 * @description TODO
 */
public class Solution160 {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        while (headA != null) {
            set.add(headA);
            headA = headA.next;
        }
        while (headB != null) {
            if(set.contains(headB)){
                return headB;
            }
            headB = headB.next;
        }
        return null;
    }

//    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//        if (headA == null || headB == null) {
//            return null;
//        }
//        ListNode pA = headA, pB = headB;
//        while (pA != pB) {
//            pA = pA == null ? headB : pA.next;
//            pB = pB == null ? headA : pB.next;
//        }
//        return pA;
//    }
//}
}

